// 
// Soapi JavaScript Library RC2 Version 0.9 Revision 2010.06.29.001
// http://soapi.info/
// 
// Copyright 2010, Sky Sanders
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://soapi.info/code/js/license.aspx
// 
// Date: Thu July 01 2010
//     
/// <reference path="../3rd-party/jquery-1.4.1.js" />
/// <reference path="../3rd-party/qunit.runner.js" />
/// <reference path="Soapi.js" />






module("path length");
asyncTest("get 100 users in one request", function()
{
    Soapi.RouteFactory("api.stackoverflow.com", apiKey)
    .Users({ pagesize: 100, page: 1 })
    .getResponse(function(data)
    {
        var userIds = [];
        $.each(data.items, function(idx, user)
        {
            userIds.push(user.user_id);
        });

        Soapi.RouteFactory("api.stackoverflow.com", apiKey)
        .UsersById(userIds, { pagesize: 100 })
        .getResponse(function(data)
        {
            ok(data.total == 100 && data.items.length == 100, "total:" + data.total + " count:" + data.items.length);
            start();
        }, function(error)
        {

            ok(false, error.message);
            start();
        });



    }, function(error)
    {
        ok(false, error.message);
        start();
    });
});

 



module("date conversion");
asyncTest("min/max as JavaScript Date", function()
{
    expect(1);

    // here are some date objects
    var fromDate = new Date("1 Jun 2010 00:00:00");
    var toDate = new Date("1 Jun 2010 01:00:00");

    // get some questions


    // use min/max parameters with date objects
    // dates are converted to unix timestamps to build the url

    Soapi.RouteFactory("api.stackoverflow.com", "qgAq_KfDu0KYzlNG-qaTuw").Questions().applyParameters({
        min: fromDate,
        max: toDate,
        sort: Soapi.Domain.PostSort.creation,
        pagesize: 1
    })
    .getResponse(function(data)
    {
        // unix timestamps are converted to Date in response before you get them
        var date = data.items[0].creation_date;
        ok(date >= fromDate && date <= toDate, date);

        start();
    }, function(error)
    {
        ok(false, err.message);
        start();

    });
});



asyncTest("todate/fromdate as JavaScript Date", function()
{
    expect(1);

    // here are some date objects
    var fromDate = new Date("1 Jun 2010 00:00:00");
    var toDate = new Date("1 Jun 2010 01:00:00");

    // get some questions


    // use min/max parameters with date objects
    // dates are converted to unix timestamps to build the url

    Soapi.RouteFactory("api.stackoverflow.com", "qgAq_KfDu0KYzlNG-qaTuw").Questions().applyParameters({
        fromdate: fromDate,
        todate: toDate,
        sort: Soapi.Domain.PostSort.creation,
        pagesize: 1
    })
    .getResponse(function(data)
    {
        // unix timestamps are converted to Date in response before you get them
        var date = data.items[0].creation_date;
        ok(date >= fromDate && date <= toDate, date);

        start();
    }, function(error)
    {
        ok(false, err.message);
        start();

    });
});



asyncTest("todate/fromdate as string", function()
{
    expect(1);

    // here are some date string
    var fromDate = "1 Jun 2010 00:00:00";
    var toDate = "1 Jun 2010 01:00:00";

    // get some questions

    Soapi.RouteFactory("api.stackoverflow.com", "qgAq_KfDu0KYzlNG-qaTuw").Questions().applyParameters({
        fromdate: fromDate,
        todate: toDate,
        sort: Soapi.Domain.PostSort.creation,
        pagesize: 1
    })
    .getResponse(function(data)
    {
        // unix timestamps are converted to Date in response before you get them
        var date = data.items[0].creation_date;
        ok(date >= new Date(fromDate) && date <= new Date(toDate), date);

        start();
    }, function(error)
    {
        ok(false, err.message);
        start();

    });

});



module("applyIdArguments");
asyncTest("Ensure ParamArray id", function()
{
    expect(2);

    var c = Soapi.RouteFactory('api.stackapps.com', apiKey).UsersById(1, 2, 3, 4, 14);
    c.page = 1;
    c.pagesize = 6;

    c.getResponse(function(data)
    {
        ok(true, 'async call succeeded');
        ok(data.total == 5, "correct total - paramarray ok");
        start();
    }, function(err)
    {
        ok(false, err.message);
        start();
    }, 5000);
});



module("StackAuth Support");

asyncTest("Sites", function()
{

    expect(2);

    // A target site is not required for stackauth methods
    // You can use any factory instance or create one with
    // an empty target.
    var route = Soapi.RouteFactory('', apiKey).Sites();

    route.getResponse(function(data)
    {
        ok(true, 'async call succeeded');
        ok(data.items.length > 0);
        start();
    }, function(err)
    {
        ok(false, err.message);
        start();
    }, 5000);
});


asyncTest("User Associated Accounts", function()
{

    expect(2);

    // A target site is not required for stackauth methods
    // You can use any factory instance or create one with
    // an empty target.
    var route = Soapi.RouteFactory('', apiKey)
        .UsersByIdAssociated("e58345f5-0f7b-4261-b449-3959c596f91f");

    route.getResponse(function(data)
    {
        ok(true, 'async call succeeded');
        ok(data.items.length > 0);
        start();
    }, function(err)
    {
        ok(false, err.message);
        start();
    }, 5000);
});




module("AnswersById");

/// Gets a set of questions enumerated in id.
asyncTest("AnswersByIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).AnswersById(2917525)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                body: false,
                comments: false
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});


asyncTest("AnswersByIdSanityCheck2", function()
{

    expect(1);
    var factory = Soapi.RouteFactory('api.stackoverflow.com', apiKey);

    var route = factory.AnswersById(2917525, 2701766);

    route.applyParameters({
        //id is set in factory method call
        // these are default values that do not result in a 
        // parameter being added to the url
        body: false,
        comments: false
    });





    route.getResponse(
      function(data)
      {
          ok(data.items.length == 2, 'async call succeeded, question count inaccurate');
          start();
      },
      function(err)
      {
          ok(err.message);
          start();
      }, 5000);
});


asyncTest("AnswersByIdSanityCheck3", function()
{
    expect(1);
    var factory = Soapi.RouteFactory('api.stackoverflow.com', apiKey);
    var route = factory.AnswersById(2917525, 2701766);

    route.body = false;
    route.comments = false;


    route.getResponse(
      function(data)
      {
          ok(data.items.length == 2, 'async call succeeded, question count inaccurate');
          start();
      },
      function(err)
      {
          ok(err.message);
          start();
      }, 5000);
});

//////////////////////////////////

module("AnswersByIdComments");

/// Gets the comments associated with a set of answers.
asyncTest("AnswersByIdCommentsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).AnswersByIdComments(2907216)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("Badges");

/// Gets all badges, in alphabetical order.
asyncTest("BadgesSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).Badges()
            .applyParameters({

        })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("BadgesById");

/// Gets the users that have been awarded the badges identified in 'id'.
asyncTest("BadgesByIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).BadgesById(9)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("BadgesName");

/// Gets all standard, non-tag-based badges in alphabetical order.
asyncTest("BadgesNameSanityCheck", function()
{
    expect(1);


    Soapi.RouteFactory('api.stackoverflow.com', apiKey).BadgesName()
            .applyParameters({

        })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("BadgesTags");

/// Gets all tag-based badges in alphabetical order.
asyncTest("BadgesTagsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).BadgesTags()
            .applyParameters({

        })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("CommentsById");

/// Gets comments by ids.
asyncTest("CommentsByIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).CommentsById(2917319)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////
// this test fails when run locally, e.g. not being served. I consider that noise for now.
// it is testing an artificial error.
//module("ErrorsById");

///// Simulates an error given its code
module("ErrorsById");

asyncTest("NotFound", function()
{
    expect(1);
    var code = 404;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InternalServerError", function()
{
    expect(1);
    var code = 500;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InvalidApplicationPublicKey ", function()
{
    expect(1);
    var code = 4000;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InvalidPageSize ", function()
{
    expect(1);
    var code = 4001;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InvalidSort ", function()
{
    expect(1);
    var code = 4002;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InvalidOrder ", function()
{
    expect(1);
    var code = 4003;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("RequestLimitExceeded ", function()
{
    expect(1);
    var code = 4004;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InvalidVectorFormat ", function()
{
    expect(1);
    var code = 4005;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("TooManyIds ", function()
{
    expect(1);
    var code = 4006;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("UnconstrainedSearch", function()
{
    expect(1);
    var code = 4007;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


asyncTest("InvalidTags ", function()
{
    expect(1);
    var code = 4008;
    Soapi.RouteFactory('api.stackoverflow.com', apiKey)
    .ErrorsById(code)
    .getResponse(function(data)
    {
        ok(false, 'async call succeeded - should have failed with ' + code);
        start();
    }, function(err)
    {
        ok(err.code = code);
        start();
    });

});


//////////////////////////////////

module("PostsByIdComments");

/// Gets the comments associated with a set of posts (questions and/or answers).
asyncTest("PostsByIdCommentsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).PostsByIdComments(2912300)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("Questions");

/// Gets question summary information.
asyncTest("QuestionsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).Questions()
            .applyParameters({
                tagged: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("QuestionsById");

/// Gets the set questions identified in 'id' and their answers.
asyncTest("QuestionsByIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).QuestionsById(2912300)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("QuestionsByIdAnswers");

/// Gets any answers to the question in 'id'.
asyncTest("QuestionsByIdAnswersSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).QuestionsByIdAnswers(2912300)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("QuestionsByIdComments");

/// Gets the comments associated with a set of questions.
asyncTest("QuestionsByIdCommentsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).QuestionsByIdComments(2912300)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("QuestionsByIdTimeline");

/// Gets the timeline of events for the questions in 'id'.
asyncTest("QuestionsByIdTimelineSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).QuestionsByIdTimeline(2912300)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////


module("QuestionsUnanswered");

/// Gets questions that have no upvoted answers.
asyncTest("QuestionsUnansweredSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).QuestionsUnanswered()
            .applyParameters({
                tagged: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 10000);

});

//////////////////////////////////

module("Stats");

/// Gets various system statistics.
asyncTest("StatsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).Stats()
            .applyParameters({

        })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("Tags");

/// Gets the tags on all questions, along with their usage counts.
asyncTest("TagsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).Tags()
            .applyParameters({
                filter: "",
                max: "",
                min: "",
                order: "",
                sort: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("Users");

/// Gets user summary information.
asyncTest("UsersSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).Users(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                filter: "",
                sort: "",
                order: "",
                min: "",
                max: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersById");

/// Gets summary information for a set of users.
asyncTest("UsersByIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersById(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: "",
                max: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdAnswers");

/// Gets answer summary information for a set of users.
asyncTest("UsersByIdAnswersSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdAnswers(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdBadges");

/// Gets the badges that have been awarded to a set of users.
asyncTest("UsersByIdBadgesSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdBadges(242897)
            .applyParameters({
            // can be set in constructor using paramarray
            //id: ""            
        })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdComments");

/// Gets the comments that a set of users have made.
asyncTest("UsersByIdCommentsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdComments(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdCommentsByToId");

/// Gets the comments by a set of users that mention the user with 'toid'.
asyncTest("UsersByIdCommentsByToIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdCommentsByToId(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0,
                toid: 160173
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdFavorites");

/// Gets summary information for the questions that have been favorited by a set of users.
asyncTest("UsersByIdFavoritesSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdFavorites(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdMentioned");

/// Gets comments that are directed at a set of users.
asyncTest("UsersByIdMentionedSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdMentioned(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdQuestions");

/// Gets question summary information for a set of users.
asyncTest("UsersByIdQuestionsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdQuestions(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                body: false,
                comments: false,
                sort: "",
                order: "",
                min: 0,
                max: 0,
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdReputation");

/// Gets information on reputation changes for a set of users.
asyncTest("UsersByIdReputationSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdReputation(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdTags");

/// Gets the tags that a set of users has participated in.
asyncTest("UsersByIdTagsSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdTags(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                filter: "",
                max: "",
                min: "",
                order: "",
                sort: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("UsersByIdTimeline");

/// Gets actions a set of users have performed.
asyncTest("UsersByIdTimelineSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).UsersByIdTimeline(242897)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                fromdate: 0,
                todate: 0,
                page: 0,
                pagesize: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////


// these tests are latent and placed at the end so that my ad-hoc test farm (browsershots.org)
// can get a good sampling before timeout

//////////////////////////////////

module("RevisionsById");

/// Gets the post history revisions for a set of posts in 'id'.
asyncTest("RevisionsByIdSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).RevisionsById(2688191)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                fromdate: 0,
                todate: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("RevisionsByIdByRevisionGuid");

/// Get a specific post revision.
asyncTest("RevisionsByIdByRevisionGuidSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).RevisionsByIdByRevisionGuid(2688191)
            .applyParameters({
                // can be set in constructor using paramarray
                //id: "",
                revisionguid: "bbf1b5f7-3605-4672-82eb-e029e64f440b",
                fromdate: 0,
                todate: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 5000);

});

//////////////////////////////////

module("Search");

/// Searches questions.  One of intitle, tagged, or nottagged must be set.
/// 
/// Searches that are purely text based should be routed through a third-party search engine,
/// for performance reasons.
asyncTest("SearchSanityCheck", function()
{
    expect(1);

    Soapi.RouteFactory('api.stackoverflow.com', apiKey).Search()
            .applyParameters({
                tagged: "",
                nottagged: "",
                intitle: "stackoverflow",
                page: 0,
                pagesize: 0,
                sort: "",
                order: "",
                min: 0,
                max: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                start();
            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 25000);

});


module("cacheing");
asyncTest("repeated search", function()
{
    expect(200);
    // this should be cached and 100 requests should take less than 1 second
    var interval = new Date().getTime() + 2000;
    var count = 100;
    function inner()
    {
        Soapi.RouteFactory('api.stackoverflow.com', apiKey).Search()
            .applyParameters({
                tagged: "",
                nottagged: "",
                intitle: "stackoverflow",
                page: 0,
                pagesize: 0,
                sort: "",
                order: "",
                min: 0,
                max: 0
            })
            .getResponse(function(data)
            {
                ok(true, 'async call succeeded');
                ok(interval > new Date().getTime(), "pulled from cache < 2 seconds");
                if (--count == 0)
                {
                    start();
                }
                else
                {
                    inner();
                }

            }, function(err)
            {
                ok(false, err.message);
                start();
            }, 25000);

    }
    inner();
});






module("throttled queue");
asyncTest("get 30 user associations", function()
{
    Soapi.RouteFactory("api.stackoverflow.com", apiKey)
    .Users()
    .getResponse(function(data)
    {
        var count = 0;
        $.each(data.items, function(idx, itm)
        {
            if (itm.association_id)
            {
                count++;
            }
        });
        for (var i = 0; i < data.items.length; i++)
        {
            var id = data.items[i].association_id;

            if (id)
            {
                Soapi.RouteFactory("", apiKey)
                .UsersByIdAssociated(id)
                .getResponse(function(data)
                {
                    ok(data.items.length > 0);
                    count -= 1;
                    if (count == 0)
                    {
                        start();
                    };
                }, function(error)
                {
                    ok(false, error.message);
                });
            }
        };
    }, function(error)
    {
        ok(false, error.message);
        start();
    });
});
